package model
{
	import actions.WBAction;
	
	import mx.collections.ArrayCollection;

	public class WhiteBoardModel
	{
		private var _listOfObjects:ArrayCollection;
		
		public function WhiteBoardModel()
		{
			_listOfObjects = new ArrayCollection();
		}

		public function get listOfObjects():ArrayCollection
		{
			return _listOfObjects;
		}

		public function set listOfObjects(value:ArrayCollection):void
		{
			_listOfObjects = value;
		}
		
		/**
		 * Searches for an object on the board by uid and, if it finds it, it returns it,
		 * otherwise returns null
		 */
		public function getObjectByUID(uid:String):WBObject
		{
			for each(var obj:WBObject in _listOfObjects)
			{
				if(uid == obj.uid)
					return obj;
			}
			return null;
		}
		
		/**
		 * Adds an object to the collection of objects on board
		 */
		public function addObject(object:WBObject):void
		{
			if(_listOfObjects == null)
			{
				_listOfObjects = new ArrayCollection();
			}
			_listOfObjects.addItem(object);
		}
		
		/**
		 * Removes an object from the list of objects on board
		 */
		public function removeObject(object:WBObject):void
		{
			for each (var obj:WBObject in _listOfObjects)
			{
				if(obj.uid == object.uid)
				{
					_listOfObjects.removeItemAt(_listOfObjects.getItemIndex(obj));	
				}
			}
		}
		
		public function resizeObject(object:WBObject, deltaW:Number, deltaH:Number):void
		{
			object.width += deltaW;
			object.height += deltaH;
		}
		
		/**
		 * This method syncs the objects on the board.
		 * The array - listOfObjects - is a list of objects representing the model of one of the user peers
		 */
		public function syncBoard(listOfObjects:Array):void
		{
			for(var i:int = 0; i < listOfObjects.length; i++)
			{
				var obj:WBObject = getObjectByUID((listOfObjects[i] as WBObject).uid);
				if(!obj)
				{
					addObject((listOfObjects[i] as WBObject));
				}
			}
		}

	}
}